package com.lujianfei.powerinput.ui.noteList.presenter;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.lujianfei.powerinput.database.NoteItemFieldModel;
import com.lujianfei.powerinput.database.NoteItemModel;
import com.lujianfei.powerinput.database.TemplateFieldModel;
import com.lujianfei.powerinput.database.TemplateModel;
import com.lujianfei.powerinput.ui.noteList.base.NoteListAddContract;
import com.lujianfei.powerinput.ui.noteList.base.NoteListEditContract;
import com.lujianfei.powerinput.ui.template.TemplateAddTypeActivity;
import com.lujianfei.powerinput.utils.ConfigUtils;
import com.lujianfei.powerinput.widget.input.base.IWidgetInput;
import com.lujianfei.powerinput.widget.input.factory.InputFactory;

import java.util.ArrayList;
import java.util.List;

public class NoteListEditPresenter implements NoteListEditContract.Presenter {
    private final static String TAG = "NoteListAddPresenter";
    private static final int REQUEST_ADD_TYPE = 100;
    private final NoteListEditContract.View view;
    private NoteItemModel data;

    public NoteListEditPresenter(NoteListEditContract.View view) {
        this.view = view;
    }

    @Override
    public void init() {
        if (null!=data) {
            IWidgetInput textInput = InputFactory.newInput("com.lujianfei.powerinput.widget.input.TextInput", view.currentActivity());
            if(null!=textInput) {
                textInput.setWidgetLabel("名称:");
                textInput.setWidgetValue(data.getNoteName());
                view.getContainer().addView(textInput.getWidgetView(), view.getContainer().getChildCount() - 1);

            List<NoteItemFieldModel> noteItemFieldModels = NoteItemFieldModel.find(NoteItemFieldModel.class, "noteItemId=?", "" + data.getId());

            if (null!=noteItemFieldModels) {
                for (NoteItemFieldModel fieldModel :
                        noteItemFieldModels) {
                    IWidgetInput textInputNew = InputFactory.newInput(fieldModel.getClassName(), view.currentActivity());
                    textInputNew.setOnWidgetDeleteListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view2) {
                            //移除视图
                            view.getContainer().removeView(view2);
                            //删除数据
                            IWidgetInput deleteWidget = (IWidgetInput) view2;
                            NoteItemFieldModel tag = (NoteItemFieldModel) deleteWidget.getTag();
                            tag.delete();
                        }
                    });
                    textInputNew.showDelete();
                    textInputNew.setTag(fieldModel);
                    textInputNew.setWidgetLabel(fieldModel.getWidgetName());
                    textInputNew.setWidgetValue(fieldModel.getKey());
                    view.getContainer().addView(textInputNew.getWidgetView(), view.getContainer().getChildCount() - 1);
                }
            }
            }
        }
    }

    @Override
    public void addRow() {
        view.currentActivity().startActivityForResult(new Intent(view.currentActivity(),TemplateAddTypeActivity.class),REQUEST_ADD_TYPE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent dataIntent) {
        if(resultCode!=Activity.RESULT_OK){
            return;
        }
        switch (requestCode){
            case REQUEST_ADD_TYPE:{
                ConfigUtils.ConfigEntity configEntity = (ConfigUtils.ConfigEntity) dataIntent.getSerializableExtra("data");
                final IWidgetInput textInput = InputFactory.newInput("com.lujianfei.powerinput.widget.input.TextInput",view.currentActivity());
                if (null!=textInput) {
                    textInput.showDelete();
                    textInput.setWidgetLabel(configEntity.getName());
                    textInput.setWidgetClassName(configEntity.getClassName());
                    textInput.setOnWidgetDeleteListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view2) {
                            view.getContainer().removeView(view2);
                            IWidgetInput textInput2Delete = (IWidgetInput) view2;
                            NoteItemFieldModel tag = (NoteItemFieldModel) textInput2Delete.getTag();
                            tag.delete();
                        }
                    });
                    NoteItemFieldModel noteItemFieldModel = new NoteItemFieldModel();
                    noteItemFieldModel.setWidgetName(textInput.getWidgetName());
                    noteItemFieldModel.setClassName(textInput.getWidgetClassName());
                    noteItemFieldModel.setNoteItemId(data.getId());
                    noteItemFieldModel.setKey("字段" + view.getContainer().getChildCount());
                    textInput.setTag(noteItemFieldModel);
                    noteItemFieldModel.save();
                    view.getContainer().addView(textInput.getWidgetView(), view.getContainer().getChildCount() - 1);
                }
            }
            break;
            default:
                break;
        }
    }

    private List<IWidgetInput> getChilds(){
        int childCount = view.getContainer().getChildCount();
        List<IWidgetInput> widgetInputs = new ArrayList<>();
        for (int i = 0; i < childCount; i++) {
            View childAt = view.getContainer().getChildAt(i);
            if (childAt instanceof IWidgetInput){
                IWidgetInput childAtNew = (IWidgetInput) childAt;
                if(i>0) {
                    widgetInputs.add(childAtNew);
                }
            }
        }
        return widgetInputs;
    }
    private void viewToDb(long noteItemId) {
        List<IWidgetInput> childs = getChilds();
        for (IWidgetInput widgetInput:
                childs) {
            NoteItemFieldModel noteItemFieldModel = (NoteItemFieldModel) widgetInput.getTag();
            noteItemFieldModel.setKey(widgetInput.getWidgetValue());
            noteItemFieldModel.setNoteItemId(noteItemId);
            noteItemFieldModel.save();
        }
    }

    @Override
    public void setData(NoteItemModel data) {
        this.data = data;
    }

    @Override
    public void onTitleRightImageClick() {
        viewToDb(data.getId());
        view.currentActivity().finish();
    }

}
